// Copyright  2012 Petr TALLA. [petr.talla@gmail.com]
// This file is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License
// as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This file is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY;  without even the implied warranty
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU Lesser General Public License for more details.
// [http://www.gnu.org/copyleft/lesser.html]
//=============================================================================
package tc.map;

import java.awt.Window;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.*;
import javax.swing.JPanel;

//=============================================================================
/**Open street map view implementation.
*/
public class TcMview extends JPanel {
//=============================================================================	
private TcMviewModel tilesAdmin;
private Listener listener;
private TcMviewControl controller;

public TcMview(Window owner)
{  tilesAdmin = new TcMviewModel(this);
   controller = new TcMviewControl(this, tilesAdmin);
   listener = new Listener(this);
   addMouseListener((MouseListener) listener); 
   addMouseMotionListener((MouseMotionListener) listener); 
   addComponentListener((ComponentListener) listener);
}

//=============================================================================
public TcMviewControl getController() {
	return controller;
}

public void paint(Graphics g)
{	g.clearRect(0,0,2000,2000);
	tilesAdmin.draw(g);
}

/**TcMview events handler
*/
class Listener implements  ComponentListener, MouseListener, MouseMotionListener {
	protected TcMview view;

	private boolean pointEntered;
	private Point point;
	
	public Listener(TcMview view) {
	    this.view = view;
	    pointEntered = false;
	    prev = false;
	    prev1 = new Point();
	    prev2 = new Point();
	}
	
	public void componentResized(ComponentEvent e) {
		TcTileAddress center = tilesAdmin.getCenterAddress();
		tilesAdmin.setCenter( center.getLayer(), center.getHor(), center.getVer() );
	}
	public void componentHidden(ComponentEvent e) {}	
	public void componentMoved(ComponentEvent e) {} 
	public void componentShown(ComponentEvent e) {} 
	
	/**Move command
	*/
	public void mousePressed(MouseEvent e) {
		if (pointEntered ) {
			view.tilesAdmin.moveCenter( e.getPoint().x - point.x,
	                               e.getPoint().y - point.y );
			pointEntered = false;
			if (prev) drawPrev();
			prev = false;
	    }
	    else {
	    	point = e.getPoint(); 
	      	pointEntered = true;
	    }
	    
	    view.repaint();
	} 

	public void mouseReleased(MouseEvent e) {}    
	public void mouseDragged(MouseEvent e) {}
	public void mouseClicked(MouseEvent e) {}
	public void mouseEntered(MouseEvent e) {}  
	public void mouseExited(MouseEvent e) {}
	
	private boolean prev;
	private Point prev1, prev2;
	
	/**Arrow drawing for move command. 
     */
	private void drawPrev()
	{	Graphics g = view.getGraphics();
		g.setXORMode(view.getBackground());
		if ( prev == false ) return;
		g.drawLine(prev1.x, prev1.y, prev2.x, prev2.y);
		
		Tc2dVector v = new Tc2dVector(prev1, prev2);
		v.setSize(-20.0);
					
		Tc2dVector left = v.perpendLeft();
		left.setSize(12.0);
		Point leftP = new Point(prev2);
		v.addTo(leftP);
		left.addTo(leftP);
		
		Tc2dVector right = v.perpendRight();
		right.setSize(12.0);
		Point rightP = new Point(prev2);
		v.addTo(rightP);
		right.addTo(rightP);
		
		g.drawLine(prev2.x, prev2.y, leftP.x,  leftP.y);
		g.drawLine(prev2.x, prev2.y, rightP.x, rightP.y);
		
		g.setPaintMode();
	}

	/**Arrow display for signaling the move command running. TODO.
	*/
	public void mouseMoved(MouseEvent e) {
		if ( pointEntered == false ) return;
		drawPrev();
		prev1.x = point.x;
		prev1.y = point.y;
		Point p = e.getPoint();
		prev2.x = p.x;
		prev2.y = p.y;
		prev = true;
		drawPrev();
	}     
}
//=============================================================================
}


